package ch.zbw.timeline.presentation.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import ch.zbw.timeline.domain.HolidayFormula;
import ch.zbw.timeline.presentation.ClientEventListener;

/**
 * Administration panel for the single holiday formula
 * 
 * @author Daniel Stefanovic
 * @version 18.01.2012
 * 
 */
public class AdminHolidaySinglePanel extends JPanel {

	private ClientEventListener listener;
	private JLabel lblDescription = new JLabel("Bezeichnung");
	private JLabel lblFormula = new JLabel("Formel");
	private JTextField txtDescription = new JTextField();
	private JTextField txtFormula = new JTextField();
	private JButton btnSave = new JButton("Speichern");
	private boolean editMode;
	private HolidayFormula holidayFormula;

	/**
	 * Constructor
	 */
	public AdminHolidaySinglePanel(){
		this.editMode = false;
		init();
	}
	
	/**
	 * Constructor
	 * 
	 * @param editMode enable edit mode?
	 */
	public AdminHolidaySinglePanel(boolean editMode) {
		this.editMode = editMode;
		init();
	}

	/**
	 * Initialisation
	 */
	private void init() {
		JPanel wrapper = new JPanel();
		JPanel settingPanel = new JPanel();

		String borderText = editMode ? "Feiertag bearbeiten" : "Neuen Feiertag erfassen";
		Border loweredEtched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		wrapper.setBorder(BorderFactory.createTitledBorder(loweredEtched, borderText));
		wrapper.setLayout(null);

		btnSave.setBounds(200, 100, 200, 25);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (validateUserInputs()){
					saveHolidayFormula();
				}
			}
		});

		settingPanel.setBounds(100, 30, 400, 60);
		settingPanel.setLayout(new GridLayout(2, 2, 5, 5));

		settingPanel.add(lblDescription);
		settingPanel.add(txtDescription);
		settingPanel.add(lblFormula);
		settingPanel.add(txtFormula);
		wrapper.add(settingPanel);
		wrapper.add(btnSave);


		this.setLayout(new BorderLayout());
		this.add(wrapper);
	}

	/**
	 * Set the holiday formula to edit
	 * 
	 * @param holidayFormula holiday formula
	 */
	public void setHolidayFormula(HolidayFormula holidayFormula) {
		this.holidayFormula = holidayFormula;
		loadData(holidayFormula);
	}
	
	/**
	 * Loads the selected formula in the form
	 * 
	 * @param holidayFormula
	 *            Process to load
	 */
	private void loadData(HolidayFormula holidayFormula) {
		if (holidayFormula != null) {
			this.txtDescription.setText(holidayFormula.getDescription());
			this.txtFormula.setText(holidayFormula.getFormula());
		}
	}

	/**
	 * Adds an EventListener to the panel
	 * 
	 * @param listener
	 *            EventListener
	 */
	public void addEventListener(ClientEventListener listener) {
		this.listener = listener;
	}

	private boolean validateUserInputs() {
		boolean valid = true;
		Color red = new Color(255, 0, 0);
		Color white = new Color(255, 255, 255);

		// description valide?
		if (txtDescription.getText().trim().length() > 0) {
			txtDescription.setBackground(white);
		} else {
			valid = false;
			txtDescription.setBackground(red);
		}
		
		// formula valide?
		String formula = txtFormula.getText().trim();
		String patternYear = "^(\\d\\d\\.\\d\\d\\.YYYY)$";
		String patternEaster = "^(OSTERN)|(OSTERN[+-]\\d)$";
		if (formula.matches(patternYear) || formula.matches(patternEaster)) {
			txtFormula.setBackground(white);
		} else {
			valid = false;
			txtFormula.setBackground(red);
		}
		
		return valid;
	}

	/**
	 * Save the current holiday formula
	 */
	private void saveHolidayFormula() {
		if(!editMode){
			holidayFormula = new HolidayFormula();
		}
		
		holidayFormula.setDescription(txtDescription.getText().trim());
		holidayFormula.setFormula(txtFormula.getText().trim());
		
		listener.saveHolidayFormulaButtonPressed(editMode, holidayFormula);

		this.clearAllFields();
	}
	
	/**
	 * Clear all input fields
	 */
	private void clearAllFields() {
		holidayFormula = null;
		txtDescription.setText("");
	}
}
